import Vue from "vue";
import $web3js from "../web3.js";
import $erc20Token from "../erc20Token/index.js";

const ifilJointUContract = require("./ifilJointUContract.json");

let contractAddress = "";
let web3 = $web3js.getWeb3();
let contractConst = {
  contract: web3
    ? new web3.eth.Contract(ifilJointUContract.abi, contractAddress)
    : ""
};
const getIfilJointUAddress = () => {
  return contractAddress;
};

const getIfilJointUContract = () => {
  return contractConst.contract;
};

const jointUUserDeposit = async (_pid, _amount, _contractAddress) => {
  _pid = $web3js.stringToBytes32(_pid);
  _amount = await $erc20Token.toErc20TokenValue(_amount, _contractAddress);
  return $web3js.commonSend(
    getIfilJointUContract().methods.userDeposit,
    _pid,
    _amount
  );
};

const jointUUserDepositEstimateGas = async (
  _pid,
  _amount,
  _contractAddress
) => {
  _pid = $web3js.stringToBytes32(_pid);
  _amount = await $erc20Token.toErc20TokenValue(_amount, _contractAddress);
  return $web3js.commonEstimateGas(
    getIfilJointUContract().methods.userDeposit,
    _pid,
    _amount
  );
};

const jointUUserIncome = _pid => {
  _pid = $web3js.stringToBytes32(_pid);
  return $web3js.commonSend(getIfilJointUContract().methods.userIncome, _pid);
};

const jointUUserIncomeEstimateGas = _pid => {
  _pid = $web3js.stringToBytes32(_pid);
  return $web3js.commonEstimateGas(
    getIfilJointUContract().methods.userIncome,
    _pid
  );
};

const jointUVendorWithdraw = _pid => {
  _pid = $web3js.stringToBytes32(_pid);
  return $web3js.commonSend(
    getIfilJointUContract().methods.vendorWithdraw,
    _pid
  );
};

const jointUVendorWithdrawEstimateGas = _pid => {
  _pid = $web3js.stringToBytes32(_pid);
  return $web3js.commonEstimateGas(
    getIfilJointUContract().methods.vendorWithdraw,
    _pid
  );
};

const getJointUPoolInfo = (_pid, ptoken, dli) => {
  _pid = $web3js.stringToBytes32(_pid);
  return $web3js.commonCallAndHandlerParams(
    getIfilJointUContract().methods.getMiningPoolInfo,
    handlePoolInfo,
    [ptoken, dli],
    _pid
  );
};

const handlePoolInfo = async (res, ptoken, dli) => {
  // console.log(res)
  let index = 0;
  let totalPrincipalAmount = res[index++];
  let totalBenefitAmount = res[index++];
  let principalLiquidAmount = res[index++];
  let benefitLiquidAmount = res[index++];
  let principalExchangeAmount = res[index++];
  let benefitExchangeAmount = res[index++];
  let dpPrice = res[index++];

  totalPrincipalAmount = await $erc20Token.fromErc20TokenValue(
    totalPrincipalAmount,
    ptoken
  );
  totalBenefitAmount = await $erc20Token.fromErc20TokenValue(
    totalBenefitAmount,
    dli
  );
  principalLiquidAmount = await $erc20Token.fromErc20TokenValue(
    principalLiquidAmount,
    ptoken
  );
  benefitLiquidAmount = await $erc20Token.fromErc20TokenValue(
    benefitLiquidAmount,
    dli
  );
  principalExchangeAmount = await $erc20Token.fromErc20TokenValue(
    principalExchangeAmount,
    ptoken
  );
  benefitExchangeAmount = await $erc20Token.fromErc20TokenValue(
    benefitExchangeAmount,
    dli
  );
  dpPrice = Vue.prototype.$numberUtils.movePointLeft(dpPrice, 18);

  return {
    totalPrincipalAmount,
    totalBenefitAmount,
    principalLiquidAmount,
    benefitLiquidAmount,
    principalExchangeAmount,
    benefitExchangeAmount,
    dpPrice
  };
};

const getJointUPoolGlobalInfo = _pid => {
  return $web3js.commonCallAndHandler(
    getIfilJointUContract().methods.getGlobalInfo,
    handlePoolGlobalInfo,
    _pid
  );
};

const handlePoolGlobalInfo = async res => {
  // console.log(res)
  let index = 0;
  let ptoken = res[index++];
  let afil = res[index++];
  let dlp = res[index++];
  let dli = res[index++];
  let dpPrice = res[index++];
  let userDeposits = res[index++];
  let userGainPrincipals = res[index++];
  let userGainBenefits = res[index++];
  let plateformBenefits = res[index++];
  let vendorGains = res[index++];
  let vendorReleasePrincipals = res[index++];
  let vendorReleaseBenefits = res[index++];
  let state = res[index++];
  let active = res[index++];

  userDeposits = await $erc20Token.fromErc20TokenValue(userDeposits, ptoken);
  userGainPrincipals = await $erc20Token.fromErc20TokenValue(
    userGainPrincipals,
    ptoken
  );
  userGainBenefits = await $erc20Token.fromErc20TokenValue(
    userGainBenefits,
    dli
  );
  plateformBenefits = await $erc20Token.fromErc20TokenValue(
    plateformBenefits,
    dli
  );
  vendorGains = await $erc20Token.fromErc20TokenValue(vendorGains, ptoken);
  vendorReleasePrincipals = await $erc20Token.fromErc20TokenValue(
    vendorReleasePrincipals,
    ptoken
  );
  vendorReleaseBenefits = await $erc20Token.fromErc20TokenValue(
    vendorReleaseBenefits,
    dli
  );

  dpPrice = Vue.prototype.$numberUtils.movePointLeft(dpPrice, 18);

  return {
    ptoken,
    afil,
    dlp,
    dli,
    dpPrice,
    userDeposits,
    userGainPrincipals,
    userGainBenefits,
    plateformBenefits,
    vendorGains,
    vendorReleasePrincipals,
    vendorReleaseBenefits,
    state,
    active
  };
};

const getJointUUserIncome = _pid => {
  return $web3js.commonCallAndHandler(
    getIfilJointUContract().methods.getUserIncome,
    $web3js.fromWei,
    _pid
  );
};

export default {
  getIfilJointUAddress,
  getIfilJointUContract,
  jointUUserDeposit,
  jointUUserIncome,
  jointUUserIncomeEstimateGas,
  jointUVendorWithdraw,
  jointUVendorWithdrawEstimateGas,
  getJointUPoolInfo,
  getJointUPoolGlobalInfo,
  getJointUUserIncome
};
